﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Alex.Model;
using Alex2.Model;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;


// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Alex2
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 
    public sealed partial class BlankPage1 : Page
    {
        //private string dbName = "Chinook.db";
        private string dbName = "Alex.db";
        String naam;

        public BlankPage1()
        {
            this.InitializeComponent();
            LoadData();

        }

        private async void LoadData()
        {
            await CreateIfNotExists(dbName);
        }
        public void getThing()
        {

        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        public List<Relation> initializeWold(int place){  //get who or what needs to be initialized
           List<Relation> idCon = new List<Relation>();

            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {
                  idCon = (db.Table<Relation>().Where(c => c.name.Equals("AtLocation") && c.idConcept2 == place)).ToList();
                 //idCon.ElementAt(0).idConcept1;
            } 
            return idCon;
        
        }

        public String getName(int n)
        {
            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);

            using (var db = new SQLite.SQLiteConnection(dbPath))
            {

                var list = db.Table<Character>().OrderBy(a => a.name).ToList();
                // System.Diagnostics.Debug.WriteLine(list.Count);
                var _Character = (db.Table<Character>().Where(c => c.idCharacter == n)).Single();
                //naam = _Character.name;
                naam = _Character.pic;
                // pika.Text = naam;
            }
            return naam;
        }

        public String getItem(int n)
        {
            List<int> holdId;
            holdId = new List<int>();
            String picc;
            

            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {

                var idCon = (db.Table<Relation>().Where(c => c.name.Equals("AtLocation") && c.idConcept2 == 1 && (c.idConcept1 == 3 || c.idConcept1 == 5))).ToList();
                //var hold2 = idCon.
                //System.Diagnostics.Debug.WriteLine(idCon.Count);
                //wala pang connect
                var _Item = (db.Table<Item>().Where(c => c.idItem == n)).Single();
                picc = _Item.pic;
            }
            return picc;
        }

        public String getDefinition(int ID)
        {
            String def;
            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {
                var _Defi = (db.Table<Item>().Where(c => c.idItem == ID)).Single();
                def = _Defi.definition;
            }
            return def;
        }

        public String getConType(int ID)
        {
            String def;
            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {
                var _ConTy = (db.Table<Concept>().Where(c => c.idConcept == ID)).Single();
                def = _ConTy.Type;
            }
            return def;
        }

        public Item getItems(int itemId)
        {
            Item _item = new Item();

            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {
                _item = (db.Table<Item>().Where(c => c.idConcept == itemId)).Single();

            } return _item;
        }

        public Character getCharacters(int charId)
        {
            Character _chara = new Character();

            var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, dbName);
            using (var db = new SQLite.SQLiteConnection(dbPath))
            {
                _chara = (db.Table<Character>().Where(c => c.idConcept == charId)).Single();

            } return _chara;
        }


        public List<Item> retrieveItems(List<Relation> rela)
        {
            List<Item> idItem = new List<Item>();

            for (int i = 0; i < rela.Count; i++)
            {
                if (getConType(rela.ElementAt(i).idConcept1).Equals("Item"))
                {
                    //System.Diagnostics.Debug.WriteLine("retirieve item id  is " + rela.ElementAt(i).idConcept1); 
                    idItem.Add(getItems(rela.ElementAt(i).idConcept1));
                }
            }
            return idItem;
        }

        public List<Character> retrieveCharacter(List<Relation> rela)
        {
            List<Character> idChar = new List<Character>();

            for (int i = 0; i < rela.Count; i++)
            {
                if (getConType(rela.ElementAt(i).idConcept1).Equals("Character"))
                    idChar.Add(getCharacters(rela.ElementAt(i).idConcept1));
            }
            return idChar;
        }



        private async Task CreateIfNotExists(string dbName)
        {
            if (await GetIfFileExistsAsync(dbName) == null)
            {
                StorageFile seedFile = await StorageFile.GetFileFromPathAsync(
                Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path,
                dbName));
                await seedFile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
            }
        }

        private async Task<StorageFile> GetIfFileExistsAsync(string key)
        {
            try
            {
                return await ApplicationData.Current.LocalFolder.GetFileAsync(key);
            }
            catch (FileNotFoundException) { return default(StorageFile); }
        }
    }
}

   

